import Taro from '@tarojs/taro';
import u from 'updeep'
import request from '../util/request'

export default {
  namespace: 'deviceDetails',
  state: {
    listData: {}, // 详细信息
  },
  reducers: {
    updateState(state, {payload}) {
      return u({
        ...payload
      }, state)
    }
  },
  effects: {
    //详情
    * getDetails({payload = {}}, {select, put, call}) {
      const {
        listData
      } = yield select(({deviceDetails}) => deviceDetails)

      let url = '/service/device/' + listData.id;
      const {data} = yield call(request, {
        url: SERVERURL + url,
        method: 'get',
      })

      const d = data.data || {};
      if (data.code === '000000') {

        yield put({
          type: 'updateState',
          payload: {
            listData: d,
          }
        })
      } else {

      }
    },
    //分析
    * addView({payload = {}}, {select, put, call}) {
      const {
        listData
      } = yield select(({deviceDetails}) => deviceDetails)

      let url = '/service/device/addView?goodId=' + listData.id;

      const {data} = yield call(request, {
        url: SERVERURL + url,
        method: 'post',
      })

      if (data && data === "unauthc") {
        Taro.navigateTo({
          url: `/pages/login/index`
        })
        Taro.showToast({
          title: ("会话过时"),
          icon: 'none'
        })
      }

      if (data.code === '000000') {
        const list = listData;
        list.cellphone = data.data.cellphone
        list.showCellphone = data.data.showCellphone
        yield put({
          type: 'updateState',
          payload: {
            listData: list,
          }
        })
      } else {

      }
    }
  }
};
